App({
  login: function (_parent, page = '') {
    var _this = this;
    wx.login({
      success: resp => {
        wx.request({
          url: _this.globalData.apiDomain + 'crowdsource/wechat/checkLogin/' + resp.code,
          header: { 'Content-Type': 'application/json' },
          success: function (res) {
            if (res.data.status == 'ok') {

              if (res.data.object.userToken) {
                _this.globalData.token = res.data.object.userToken;
                console.log(res.data.object.weChatUser);
                _this.globalData.userInformation = res.data.object.weChatUser;
                //_this.globalData.userInfo = res.data.object.weChatUser;

                if (_this.userLoginReadyCallback) {
                  _this.userLoginReadyCallback(res)
                }
              } else {
                console.log('no token');
                _this.globalData.openId = res.data.object.openId;
                _this.globalData.sessionKey = res.data.object.sessionKey;
                wx.redirectTo({
                  url: '/pages/user/registration_selection/registration_selection?rollback=' + encodeURIComponent(page),
                })
              }
            } else {
              wx.showModal({
                content: '不好，挂了！管理员哥哥正在赶来~',
                showCancel: false
              })
            }
          },
          fail: function (e) {
            wx.showModal({
              content: '网络异常,请重试',
              showCancel: false
            })
          }
        })
      },
    })
  },
  responseCheck: function (_this, data, url) {
    if (data.status == 'error') {
      wx.showModal({
        content: '不好，挂了！管理员哥哥正在赶来~',
        showCancel: false
      })
    } else {
      //判断token是否失效
      if (data.status == 'token expried') {
        this.login(_this, url);
      } else if (data.status == 'audit fail') {
        wx.navigateTo({
          url: '/pages/user/auth/auth',
        });
      } else {
        wx.showModal({
          content: data.message,
          showCancel: false
        })
      }
    }
  },
  globalData: {
    loginCompleted: false,
    userInfo: null,
    apiUrl: 'https://wnear.com/',
    //apiDomain: 'http://192.168.120.134:8080/',
    apiDomain: 'http://cs-ea-web.eastasia.cloudapp.azure.com:8080/',
    //apiDomain: 'https://wincrowd.pactera.com/',
    token: null,
    openId: null,
    sessionKey: null,
    userInformation: {

    },
    incomeStatistics: {} //账户余额
  }
})